package com.doge.calcalbum.business.vip

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.core.view.isVisible
import com.doge.calcalbum.R
import com.doge.calcalbum.databinding.DialogVipLeftTimeInfo2Binding
import com.doge.calcalbum.utils.disableSkin
import com.doge.calcalbum.utils.rxInterval
import com.doge.calcalbum.utils.setOnClickCallback
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit
import kotlin.math.max

class VipLeftTimeInfoDialog private constructor(context: Context, theme: Int) :
    Dialog(context, theme) {
    private class Params {
        lateinit var mContext: Context
        var canCancel = false
        lateinit var binding: DialogVipLeftTimeInfo2Binding
        var confirmCallback: DialogInterface.OnClickListener? = null
        var leftTime: Long = 0
    }

    private var task: Disposable? = null
    private var p: Params? = null

    override fun show() {
        super.show()
        task?.dispose()
        task = rxInterval(1, 1, TimeUnit.SECONDS) {
            p?.leftTime = max(0, (p?.leftTime ?: 0) - 1000)
            update()
        }
    }

    private fun update() {
        val leftSec = (p?.leftTime ?: 0) / 1000

        val day = leftSec / (3600 * 24)
        val leftHourSec = leftSec - (day * (3600 * 24))
        val hour = leftHourSec / 3600
        val leftMinSec = leftHourSec - (hour * 3600)
        val min = leftMinSec / 60
        val sec = leftMinSec - (min * 60)
        p?.binding?.timeDay?.text = day.toString()
        p?.binding?.timeHour?.text = appendZero(hour)
        p?.binding?.timeMin?.text = appendZero(min)
        p?.binding?.timeSec?.text = appendZero(sec)
    }

    private fun appendZero(value: Long): String {
        return if (value > 9) "$value"
        else "0$value"
    }

    override fun dismiss() {
        super.dismiss()
        task?.dispose()
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.mContext = context
        }

        fun show() {
            create().show()
        }

        fun setLeftTimeSec(leftTime: Long): Builder {
            p.leftTime = leftTime
            return this
        }

        fun setCallback(
            callback: DialogInterface.OnClickListener
        ): Builder {
            p.confirmCallback = callback
            return this
        }

        fun create(): VipLeftTimeInfoDialog {
            val dialog = VipLeftTimeInfoDialog(
                p.mContext,
                R.style.Theme_Light_NoTitle_Dialog
            )
            dialog.window?.decorView?.setPadding(0, 0, 0, 0)
            val lp = dialog.window?.attributes
            lp?.width = WindowManager.LayoutParams.MATCH_PARENT
            lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window?.attributes = lp

            p.binding = DialogVipLeftTimeInfo2Binding.inflate(LayoutInflater.from(p.mContext))
            p.binding.confirm.setOnClickCallback {
                dialog.dismiss()
                p.confirmCallback?.onClick(dialog, 0)
            }
            p.binding.close.setOnClickListener {
                dialog.dismiss()
            }
            dialog.p = p


            val skinViews = arrayOf(
                p.binding.tip2Image,
                p.binding.tip2Text,
            )
            skinViews.forEach { it.isVisible = !disableSkin }

            dialog.setContentView(p.binding.root)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }
    }
}